package com.leo.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leo.blog.dao.LogDao;
import com.leo.blog.dto.LogExecution;
import com.leo.blog.entity.Log;
import com.leo.blog.enums.LogStateEnum;
import com.leo.blog.exceptions.LogOperationException;
import com.leo.blog.service.LogService;
import com.leo.blog.util.PageCalculator;

@Service
public class LogServiceImpl implements LogService{
	@Autowired
	private LogDao logDao;
	
	@Override
	public LogExecution deleteByPrimaryKey(int logId) {
		if(logId > 0) {
			int effectedNum = logDao.deleteByPrimaryKey(logId);
			if(effectedNum > 0) {
				return new LogExecution(LogStateEnum.SUCCESS);
			}else {
				return new LogExecution(LogStateEnum.OFFLINE);
			}
		}else {
			return new LogExecution(LogStateEnum.OFFLINE);
		}
	}

	@Override
	public LogExecution addLog(Log log) throws LogOperationException {
		if(log != null) {
			//初始化时间
			log.setCreateTime(new Date());
			
			int effectedNum = logDao.insertLog(log);
			if(effectedNum > 0) {
				return new LogExecution(LogStateEnum.SUCCESS);
			}else {
				return new LogExecution(LogStateEnum.OFFLINE);
			}
		}else {
			return new LogExecution(LogStateEnum.NULL_LOG);
		}
	}

	@Override
	public LogExecution addSelective(Log log) throws LogOperationException{
		if(log != null) {
			//初始化时间
			log.setCreateTime(new Date());
			
			int effectedNum = logDao.insertSelective(log);
			if(effectedNum > 0) {
				return new LogExecution(LogStateEnum.SUCCESS);
			}else {
				return new LogExecution(LogStateEnum.OFFLINE);
			}
		}else {
			return new LogExecution(LogStateEnum.NULL_LOG);
		}
	}

	@Override
	public LogExecution getByPrimaryKey(int logId) {
		if(logId > 0) {
			Log log = logDao.queryByPrimaryKey(logId);
			if(log != null) {
				return new LogExecution(LogStateEnum.SUCCESS, log);
			}else {
				return new LogExecution(LogStateEnum.OFFLINE);
			}
		}else {
			return new LogExecution(LogStateEnum.OFFLINE);
		}
	}

	@Override
	public LogExecution updateByPrimaryKeySelective(Log log) {
		if(log != null) {
			int effectedNum = logDao.updateByPrimaryKeySelective(log);
			if(effectedNum > 0) {
				return new LogExecution(LogStateEnum.SUCCESS);
			}else {
				return new LogExecution(LogStateEnum.OFFLINE);
			}
		}else {
			return new LogExecution(LogStateEnum.NULL_LOG);
		}
	}

	@Override
	public LogExecution updateByPrimaryKey(Log log) {
		if(log != null) {
			int effectedNum = logDao.updateByPrimaryKey(log);
			if(effectedNum > 0) {
				return new LogExecution(LogStateEnum.SUCCESS);
			}else {
				return new LogExecution(LogStateEnum.OFFLINE);
			}
		}else {
			return new LogExecution(LogStateEnum.NULL_LOG);
		}
	}

	@Override
	public LogExecution getLogListByDate(Log logCondition, int pageIndex, int pageSize) {
		LogExecution le = new LogExecution();
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,待用dao层返回相关类型列表
		List<Log> logList = logDao.queryLogListByDate(logCondition, rowIndex, pageSize);
		int count = logDao.queryLogListByDateCount(logCondition);
		if(logList != null && count != 0) {
			le.setLogList(logList);
			le.setCount(count);
			le.setState(LogStateEnum.SUCCESS.getState());
		}else {
			le.setState(LogStateEnum.OFFLINE.getState());
		}
		
		return le;
	}

	@Override
	public List<?> getUserLogByDate(Log logCondition) {
		if(logCondition != null) {
			return logDao.queryUserLogByData(logCondition);
		}else {
			return null;
		}
	}



	

}
